//
//  NetworkTool+extention.swift
//  WeiboThree
//
//  Created by itheima on 16/6/6.
//  Copyright © 2016年 itheima. All rights reserved.
//

import Foundation

extension RHNetWorkTool {
    
    var oauthURLString: String {
        return  "https://api.weibo.com/oauth2/authorize?client_id=2695257266&redirect_uri=\(directURL)"
    }

    /**
     获取access_token
     
     - parameter code <#code description#>
     - parameter callBack <#callBack description#>
     */
    func loadAccesstoken (code: String, callBack:([String: AnyObject]?)->()) {
        let url = "https://api.weibo.com/oauth2/access_token"
        let parmeters = ["client_id":appkey,
            "client_secret":appsecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":directURL
        ];
        
//        //请求完成回调
//        let callBack = { (response:[String: AnyObject]?) -> () in
//            callBack(response)
//        }
//        
//        request(url, method: .POST, parameters: parmeters, callBack: callBack)
        
        //调用网络中间层成功后回调
        request(url, method: .POST, parameters: parmeters) { (response:[String: AnyObject]?) in
            callBack(response)
        }
    }
    
    /**
     获取登录帐号信息
     
     - parameter accesstoken access_token 令牌
     - parameter uid 登录帐号的id
     - parameter callBack 获取用户帐号信息完成后回调
     */
    func loadUserAccount (accesstoken:String, uid: String, callBack:([String: AnyObject])?->()) {
        let url = "https://api.weibo.com/2/users/show.json"
        let parameters = ["access_token":accesstoken, "uid":uid]
        //调用网络中间层获取用户信息
        request(url, method: .GET, parameters: parameters) { (response: [String : AnyObject]?) -> () in
            callBack(response)
        }
    }
}